package com.nhn.ssi.action;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

import com.nhn.ssi.bo.TestBo;
import com.nhn.ssi.util.CharacterUtil;
import com.nhn.ssi.util.FilePathUtil;
import com.nhn.ssi.util.Pager;
import com.nhn.ssi.util.PagerService;
import com.opensymphony.xwork2.ActionSupport;

public class SearchMultiAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	TestBo testBo;
	String currentPage;
	String pagerMethod;
	String keyword;
	long useTime;
	Pager pager;
	Hits hits;
	String ct1 = "";
	String ct2 = "";
	String ct3 = "";

	public String execute() throws Exception {
		if (keyword == null || "".equals(keyword.trim())) {
			keyword = "";
			return "input";
		}
		if (keyword.contains("?") || keyword.contains("*") || keyword.contains("~") || keyword.contains("^")
			|| keyword.contains("(") || keyword.contains(")") || keyword.contains("-") || keyword.contains("+")
			|| keyword.contains("\\") || keyword.contains("!")) {
			return "input";
		}
		if ("0".equals(currentPage)) {
			currentPage = "1";
		}
		keyword = CharacterUtil.Convert(keyword.trim());
		ct1 = CharacterUtil.Convert(ct1.trim());
		ct2 = CharacterUtil.Convert(ct2.trim());
		ct3 = CharacterUtil.Convert(ct3.trim());
		String[] str_array = keyword.split(" ");
		String queryStr = "";
		for (int i = 0; i < str_array.length; i++) {
			if (!"".equals(str_array[i].trim())) {
				queryStr += "+(name:" + str_array[i].trim() + ")";
			}
		}

		searchResultHits(queryStr);

		return "searchMultiResult";
	}

	public Hits searchResultHits(String queryString) throws IOException, ParseException {
		Map<String, String> map = null;
		long startTime = new Date().getTime();
		hits = null;
		System.out.println("查找关键字【" + queryString + "】in " + FilePathUtil.INDEX_FILE_DIR + "...");
		Query query = null;
		IndexSearcher searcher = new IndexSearcher(FilePathUtil.INDEX_FILE_DIR);
		Analyzer analyzer = new StandardAnalyzer();
		try {
			QueryParser qp = new QueryParser("name", analyzer);
			query = qp.parse(queryString);
		} catch (ParseException e) {
		}
		if (searcher != null) {
			hits = searcher.search(query);

			PagerService pagerService = new PagerService();
			pager = pagerService.getPager(currentPage, pagerMethod, hits.length());
			this.setCurrentPage("" + pager.getCurrentPage());

			if (hits.length() > 0) {
				System.out.println("查找关键字【" + queryString + "】找到:" + hits.length() + " 个结果!");
				//				 测试一下索引的时间
				long endTime = new Date().getTime();
				useTime = endTime - startTime;
				System.out.println("这花费了" + useTime + "毫秒从索引中找到所要文档!");

			} else {
				System.out.println("没有找到结果!");
			}
		}
		return hits;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public TestBo getTestBo() {
		return testBo;
	}

	public void setTestBo(TestBo testBo) {
		this.testBo = testBo;
	}

	public String getPagerMethod() {
		return pagerMethod;
	}

	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public long getUseTime() {
		return useTime;
	}

	public void setUseTime(long useTime) {
		this.useTime = useTime;
	}

	public String getCt1() {
		return ct1;
	}

	public void setCt1(String ct1) {
		this.ct1 = ct1;
	}

	public String getCt2() {
		return ct2;
	}

	public void setCt2(String ct2) {
		this.ct2 = ct2;
	}

	public String getCt3() {
		return ct3;
	}

	public void setCt3(String ct3) {
		this.ct3 = ct3;
	}

	public Hits getHits() {
		return hits;
	}

	public void setHits(Hits hits) {
		this.hits = hits;
	}

}
